var c = require('../lib/common.js');
var cf = require('../lib/config.js');
Page({
  data: {
    riderinfo:'骑手正在路上...',
    rider:1,
    dis:'暂无数据',
    markers: [{
        iconPath: "/images/myaddress.png",
        id: 0,
        latitude: '',
        longitude: '',
        width: 30,
        height: 30,

      },
      {
        iconPath: "/images/motuo.png",
        id: 1,
        latitude: '',
        longitude: '',
        width: 30,
        height: 30,
        callout: {
          content: '无骑手为您服务',
          fontSize: 12,
          padding: 10,
          borderRadius: 10,
          color: '#ffffff',
          bgColor: '#444444',
          display: "ALWAYS"
        },
      }
    ],
    polyline: [{
      points: [{
        longitude: '',
        latitude: ''
      }, {
        longitude: '',
        latitude: ''
      }],
      color: "#000000",
      width: 2,
      dottedLine: true
    }],
    controls: [{
      id: 1,
      iconPath: '/images/1.jpg',
      position: {
        left: 0,
        top: 300 - 50,
        width: 50,
        height: 50
      },
      clickable: true
    }]
  },

  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  onLoad: function() {

    var that = this;
    var uid = wx.getStorageSync('userinfo').id;
    //用户本身的经纬度
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        console.log(res);
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        //获取骑手消息
        c.ajax(
          cf.hostapi + 'toUidOrderGetRiderInfo', {
            uid: uid,
            status: 3
          },
          res => {
            console.log('uidorder');
            console.log(res);
            if (res.data.retcode == 200) { //获取到了 接单骑手消息
              console.log('获取到了骑手信息');
              console.log(res);
              //用户经纬度用来 计算距离
              var lat_user = latitude;
              var lng_user = longitude;
              //骑手的经纬度  用来计算距离
              var lat_moto = res.data.data.lat;
              var lng_moto = res.data.data.lng;
              //通过上边的经纬度来计算距离
              that.distance(lat_user, lng_user, lat_moto, lng_moto);
              //设置地图上显示的两个点的 经纬度
              var user_lat = 'polyline[0].points[0].latitude';
              var user_lng = 'polyline[0].points[0].longitude';
              var moto_lat = 'polyline[0].points[1].latitude';
              var moto_lng = 'polyline[0].points[1].longitude';
              //设置用户经纬度图标
              var mark_user_w = 'markers[0].width';
              var mark_user_h = 'markers[0].height';
              var mark_user_ico = 'markers[0].iconPath';
              var mark_user_lat = 'markers[0].latitude';
              var mark_user_lng = 'markers[0].longitude';
              //设置骑手经纬度图标
              var mark_moto_w = 'markers[1].width';
              var mark_moto_h = 'markers[1].height';
              var mark_moto_ico = 'markers[1].iconPath';
              var mark_moto_lat = 'markers[1].latitude';
              var mark_moto_lng = 'markers[1].longitude';
              //设置骑手气泡
              var mark_moto_content = 'markers[1].callout.content';
              var mark_moto_padding = 'markers[1].callout.padding';
              var mark_moto_borderRadius = 'markers[1].callout.borderRadius';
              var mark_moto_color = 'markers[1].callout.color';
              var mark_moto_bgColor = 'markers[1].callout.bgColor';
              var mark_moto_display = 'markers[1].callout.display';
              var mark_moto_fontSize = 'markers[1].callout.fontSize';
              that.setData({
                [mark_moto_w]: 30,
                [mark_moto_h]: 30,
                [mark_moto_ico]: '/images/motuo.png',
                [mark_moto_content]: that.data.dis,
                [mark_moto_padding]: 10,
                [mark_moto_borderRadius]: 10,
                [mark_moto_color]: '#ffffff',
                [mark_moto_bgColor]: '#444444',
                [mark_moto_display]: 'ALWAYS',
                [mark_moto_fontSize]: 12,
                [mark_user_w]: 30,
                [mark_user_h]: 30,
                [mark_user_ico]: '/images/myaddress.png',
                //用户的经纬度赋值
                [user_lat]: lat_user,
                [user_lng]: lng_user,
                //骑手经纬度赋值
                [moto_lat]: lat_moto,
                [moto_lng]: lng_moto,
                //用户图标
                [mark_user_lat]: lat_user,
                [mark_user_lng]: lng_user,
                //骑手图标
                [mark_moto_lat]: lat_moto,
                [mark_moto_lng]: lng_moto,
                //地图中心经纬度
                longitude: lng_moto,
                latitude: lat_moto,
              });
              setTimeout(function() {
                that.onLoad()
              }, 10000)
            }else{//no rider 
              console.log('没有获取到了骑手信息');
              that.setData({ riderinfo: '暂无进行的订单', rider:0})
            }
          }

        )


      }
    })




    // var address_id_user = 10;
    // var address_id_moto = 11;
    // var that = this;
    // c.ajax(
    //   cf.hostapi + 'toIdGetAddressInfo', {
    //     id: address_id_user
    //   },
    //   res => {

    //     c.ajax(
    //       cf.hostapi + 'toIdGetAddressInfo', {
    //         id: address_id_moto
    //       },
    //       res => {


    //         // console.log(that.data);
    //       }
    //     )

    //   }
    // )




  },
  distance: function(la1, lo1, la2, lo2) {
    var content = 'markers.callout.content';
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    if (s < 1) {
      this.setData({
        dis: ('距离' + parseInt(s * 1000) + 'm')
      })
    } else {
      this.setData({
        dis: ('距离' + parseInt(s) + 'km')
      })
    }

    // console.log(that.data.polyline[0].points[0].longitude);
    // var pol ="polyline[0].points[0].longitude";
    // that.setData({ [pol]: 113.3245211 })
    // setTimeout(function(){
    //   that.setData({ [pol]: 113.3245211 })
    // },2000);
  },
  click: function() {
    var that = this;
    // console.log(that.data.markers[0].longitude[0].longitude);
    console.log(this.data);
    var pol = 'polyline[0].points[0].longitude';
    var rel = 'markers[0].longitude';
    that.setData({
      [pol]: 113.3845211,
      mi: 100,
      [rel]: 113.3845211,
      longitude: 113.3845211
    });
  },
  kefuclick: function() {

    wx.showModal({
      title: '',
      content: '4000063088',
      confirmText: '拨打',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '4000063088',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})